//
//  OTDR_SocketConnectViewController.m
//  OTDR
//
//  Created by Liu on 2024/5/23.
//

#import "OTDR_SocketConnectViewController.h"
#import "OTDR_SecondPageViewController.h"
#import "OTDR_SocketServer.h"

@interface OTDR_SocketConnectViewController ()<UITextFieldDelegate>

@property (nonatomic ,strong)  UITextField *ipTextField;
@property (nonatomic ,strong)  UITextField *portField;

@property (nonatomic ,strong)  UIButton *connectBtn;
@property (nonatomic ,strong)  UIButton *closeBtn;
@property (nonatomic ,strong)  UIButton *sendBtn;
@property (nonatomic ,strong)  UITextField *sendMsgField;
@property (nonatomic ,strong)  UILabel *recvLabel;

@property(nonatomic ,strong)OTDR_SocketServer *socketServer;
@end

@implementation OTDR_SocketConnectViewController

-(OTDR_SocketServer *)socketServer{
    if (!_socketServer) {
        _socketServer = [OTDR_SocketServer sharedInstance];
        ws(weakSelf);
        _socketServer.connectIsSuccessBlock = ^(BOOL isConnected) {
            if(isConnected){
                [ToastView tip:[LanguageTool DPLocalizedString:@"connectsuccess"]];
                //连接成功
                OTDR_SecondPageViewController *vc = [OTDR_SecondPageViewController new];
                vc.socketServer = weakSelf.socketServer;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else{
                [ToastView tip:[LanguageTool DPLocalizedString:@"connectfailed"]];
            }
        };
    }
    return _socketServer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitleStr:@""];
    self.view.backgroundColor = kWhiteColor;
    [self creatUI];
}

- (void )creatUI{
    UILabel *ipLabel = [ViewCreate createLabelFrame:CGRectMake(SIZE(20), SIZE(80), SIZE(70), SIZE(35)) backgroundColor:kWhiteColor text:@"IP" textColor:RGB51 textAlignment:Left font:FONT(17)];
    [self.view addSubview:ipLabel];
    self.ipTextField = [ViewCreate createTextFieldFrame:CGRectMake(SIZE(100), SIZE(80), SIZE(200), SIZE(35)) font:FONT(17) textColor:RGB51 placeHolder:[LanguageTool DPLocalizedString:@"ipAddr"] delegate:self];
    self.ipTextField.backgroundColor = RGB238;
    [self.view addSubview:self.ipTextField];
    [self.ipTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ipLabel.mas_right).mas_offset(SIZE(10));
        make.right.mas_offset(-SIZE(20));
        make.top.bottom.mas_equalTo(ipLabel).mas_offset(0);
    }];
    
    
    UILabel *portLabel = [ViewCreate createLabelFrame:CGRectMake(SIZE(20), SIZE(120), SIZE(70), SIZE(35)) backgroundColor:kWhiteColor text:@"PORT" textColor:RGB51 textAlignment:Left font:FONT(17)];
    [self.view addSubview:portLabel];
    self.portField = [ViewCreate createTextFieldFrame:CGRectMake(SIZE(100), SIZE(120), SIZE(200), SIZE(35)) font:FONT(17) textColor:RGB51 placeHolder:[LanguageTool DPLocalizedString:@"portID"] delegate:self];
    self.portField.backgroundColor = RGB238;
    [self.view addSubview:self.portField];
    
    [self.portField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(portLabel.mas_right).mas_offset(SIZE(10));
        make.right.mas_offset(-SIZE(20));
        make.top.bottom.mas_equalTo(portLabel).mas_offset(0);
    }];
    
    
    self.ipTextField.text = @"10.10.18.157";
    self.portField.text = @"8010";
    
    self.connectBtn = [ViewCreate createButtonFrame:CGRectMake(SIZE(20), SIZE(180), SIZE(80), SIZE(35)) title:[LanguageTool DPLocalizedString:@"connect"] titleColor:RGB51 font:FONT(17) backgroundColor:kThemeColor];
    [self.view addSubview:self.connectBtn];
    [self.connectBtn addTarget:self action:@selector(connectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(SIZE(40));
        make.top.mas_equalTo(portLabel.mas_bottom).mas_offset(SIZE(50));
        make.width.mas_offset(SIZE(100));
        make.height.mas_offset(SIZE(35));
    }];
    
    
    self.closeBtn = [ViewCreate createButtonFrame:CGRectMake(SIZE(120), SIZE(180), SIZE(80), SIZE(35)) title:[LanguageTool DPLocalizedString:@"disconnect"] titleColor:RGB51 font:FONT(17) backgroundColor:kThemeColor];
    [self.view addSubview:self.closeBtn];
    [self.closeBtn addTarget:self action:@selector(closeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-SIZE(40));
        make.top.mas_equalTo(portLabel.mas_bottom).mas_offset(SIZE(50));
        make.width.mas_offset(SIZE(100));
        make.height.mas_offset(SIZE(35));
    }];
    
//
//    self.sendMsgField = [ViewCreate createTextFieldFrame:CGRectMake(SIZE(20), SIZE(220), SIZE(200), SIZE(35)) font:FONT(17) textColor:RGB51 placeHolder:@"请输入要发送的内容" delegate:self];
//    self.sendMsgField.backgroundColor = RGB238;
//    [self.view addSubview:self.sendMsgField];
//    
//    self.sendBtn = [ViewCreate createButtonFrame:CGRectMake(SIZE(270), SIZE(220), SIZE(100), SIZE(35)) title:@"发送" titleColor:RGB51 font:FONT(17) backgroundColor:kThemeColor];
//    [self.view addSubview:self.sendBtn];
//    [self.sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.recvLabel = [ViewCreate createLabelFrame:CGRectMake(SIZE(20), SIZE(270), SIZE(300), SIZE(150)) backgroundColor:kThemeColor text:@"-" textColor:RGB51 textAlignment:Left font:FONT(17)];
//    [self.view addSubview:self.recvLabel];
}

//点击连接按钮
- (void )connectBtnAction:(UIButton *)sender {
    [self connect:self.ipTextField.text port:[self.portField.text intValue]];
}
//点击发送按钮

- (void)sendBtnAction:(UIButton *)sender {
    self.recvLabel.text = [self sendAndRecv:self.sendMsgField.text];
    NSLog(@"完毕");
}
//点击关闭按钮

- (void)closeBtnAction:(UIButton *)sender {
    BOOL isConnect = [self.socketServer isFCDisConnect];
    if (!isConnect) {
        [self.socketServer  stopConnect];
        [ToastView tip:[LanguageTool DPLocalizedString:@"disconnectsuccess"]];
    }
    else{
        [ToastView tip:[LanguageTool DPLocalizedString:@"unconnect"]];
    }
}

//1.连接服务器
- (void )connect:(NSString *)ip port:(int)port{
    BOOL isConnect = [self.socketServer isFCDisConnect];
    if (isConnect) {
        [ToastView showTitle:[LanguageTool DPLocalizedString:@"connectting"]];
        [self.socketServer connectWithHost:ip port:port];
    }
    else{
        [ToastView tip:[LanguageTool DPLocalizedString:@"connectted"]];
    }
    
}

//1.发送和接受数据
- (NSString *)sendAndRecv:(NSString *)sendMsg{
    [self.socketServer writeToFCTCPWithData:[sendMsg dataUsingEncoding:NSUTF8StringEncoding]];
    
    return @"";
}

-(void)dealloc{
    BOOL isConnect = [_socketServer isFCDisConnect];
    if (!isConnect) {
        [_socketServer  DeallocAll];
        NSLog(@"连接关闭");
    }
}

@end
